package com.dataseek.iot.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataseek.iot.sys.entity.WorkLine;
import com.dataseek.iot.sys.mapper.WorkLineMapper;
import com.dataseek.iot.sys.service.WorkLineService;
import com.dataseek.iot.sys.vo.WorkLineVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class WorkLineServiceImpl extends ServiceImpl<WorkLineMapper, WorkLine> implements WorkLineService {


    @Autowired
    private WorkLineMapper workLineMapper;

    @Override
    public WorkLineVo queryAction(String lineName, String open, Integer num, Integer size) {

        WorkLineVo lineVo = new WorkLineVo();
        QueryWrapper<WorkLine> qw = new QueryWrapper<>();
        if (lineName != null && !lineName.isEmpty()) {
            qw.like("line_name", lineName);
        }
        if (open != null && !open.isEmpty()) {
            qw.eq("open", open);
        }
        IPage<WorkLine> page = new Page<>(num, size);
        List<WorkLine> mapList = workLineMapper.selectPage(page, qw).getRecords();
        lineVo.setPage(num);
        lineVo.setSize(size);
        lineVo.setTotal(page.getTotal());
        lineVo.setList(mapList);
        return lineVo;
    }

    @Override
    public Integer insertAction(WorkLine workLine) {
        int resultCode = 0;
        QueryWrapper<WorkLine> qw = new QueryWrapper<>();
        qw.eq("line_name", workLine.getLineName()).or().eq("line_code", workLine.getLineCode());
        Integer count = workLineMapper.selectCount(qw);
        if (count == 0) {
            if (workLine.getCreateBy() == null) {
                workLine.setCreateBy("");
                workLine.setUpdateBy("");
            }
            this.saveOrUpdate(workLine);
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;

    }

    @Override
    public Integer updateAction(WorkLine workLine) {
        int resultCode = workLineMapper.updateById(workLine);
        return resultCode;
    }

    @Override
    public Integer deleteAction(List<Integer> ids) {
        int resultCode = workLineMapper.deleteBatchIds(ids);
        return resultCode;
    }

}
